
#include "HardwareSerial.h"

volatile int flag1;
volatile int flag2;
volatile int flag3;

void setMotor(int dirpin1,int dirpin2,int speedpin, int speed)
{
  if (speed == 0)
  {
    digitalWrite(dirpin1, LOW);
    digitalWrite(dirpin2, LOW);
    analogWrite(speedpin, 0);
  }
  else if (speed > 0)
  {
    digitalWrite(dirpin2, HIGH);
    digitalWrite(dirpin1, LOW);
    analogWrite(speedpin, speed);
  }
  else
  {
    digitalWrite(dirpin2, LOW);
    digitalWrite(dirpin1, HIGH);
    analogWrite(speedpin, -speed);
  }
}

void setup(){
  flag1 = 0;
  flag2 = 0;
  flag3 = 0;
  pinMode(PB0, OUTPUT);
  pinMode(PB1, OUTPUT);
  pinMode(PC5, OUTPUT);
  digitalWrite(PB0,HIGH);
  digitalWrite(PB1,HIGH);
  digitalWrite(PC5,HIGH);
  Serial1.begin(9600);
  pinMode(PC1, OUTPUT);
  pinMode(PC2, OUTPUT);
  digitalWrite(PC1, LOW);
  digitalWrite(PC2, LOW);
}

void loop(){
  if (Serial1.available() > 0) {
    if (Serial1.read() == toascii('8')) {
      flag1 = 1;
      flag2 = 0;
      flag3 = 0;

    }
    if (Serial1.read() == toascii('2')) {
      flag2 = 1;
      flag1 = 0;
      flag3 = 0;

    }
    if (Serial1.read() == toascii('5')) {
      flag3 = 1;
      flag1 = 0;
      flag2 = 0;

    }

  }
  if (flag1 == 1) {
    setMotor(PC1, PC2, PA0, 255);

  }
  if (flag2 == 1) {
    setMotor(PC1, PC2, PA0, -255);

  }
  if (flag3 == 1) {
    setMotor(PC1, PC2, PA0, 0);

  }

}
